<?php

class ActionsController extends Controller
{

	private $_model;
	public function actionCreate()
	{
		$this->render('create');
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
		{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/

	public function actionDigg(){
		if(Yii::app()->user->isGuest)NC::json(false,'登录');
		$news_id = NC::_p('dc','');
		if($news_id){
			$news_id = (int)substr($news_id,2);
			if(is_int($news_id)){
				//$actions = $this->loadModel($news_id);
				$actions = Actions::model()->exists('news_id=:news_id and user_id=:user_id',array(':news_id'=>$news_id,':user_id'=>Yii::app()->user->id));
				if(!$actions){
					if(Actions::addActions($news_id,'digg')){
						//更新nc_news表
						News::model()->updateCounters(array('digg_num'=>1),'news_id=:news_id',array(':news_id'=>$news_id));
						//更新用户表
						User::model()->updateCounters(array('share_num'=>1),'user_id=:user_id',array(':user_id'=>Yii::app()->user->id));
						$count = Actions::model()->count("news_id=:news_id and action_type!='trash'",array(':news_id'=>$news_id));
						$info = '已顶('.$count.')';
						NC::json(true,$info);
					}
				}else{
					NC::json(false,'已顶');
				}
			}
		}
	}
	public function actionTrash(){
		if(Yii::app()->user->isGuest)NC::json(false,'登录');
		$news_id = NC::_p('tr','');
		if($news_id){
			$news_id = (int)substr($news_id,2);
			if(is_int($news_id)){
				//$actions = $this->loadModel($news_id);
				$actions = Actions::model()->exists('news_id=:news_id and user_id=:user_id',array(':news_id'=>$news_id,':user_id'=>Yii::app()->user->id));
				if(!$actions){
					if(Actions::addActions($news_id,'trash')){
						//更新nc_news表
						News::model()->updateCounters(array('trash_num'=>1),'news_id=:news_id',array(':news_id'=>$news_id));
						$count = Actions::model()->count('news_id=:news_id and action_type=:action_type',array(':news_id'=>$news_id,':action_type'=>'trash'));
						$info = '已踩('.$count.')';
						NC::json(true,$info);
					}
				}else{
					NC::json(false,'已踩');
				}
			}
		}
	}
	private function loadModel($news_id){
		if(!$news_id){
			throw new CHttpException('404','您访问的页面不存在');
		}
		if($this->_model===null)
		{
			if(Yii::app()->user->isGuest){
				return 'Your must login';
			}
			$res = $this->_model=Actions::model()->find(
				'news_id=:news_id and user_id=:user_id and action_type=:type',
				array(
					':news_id'=>$news_id,
					':user_id'=>Yii::app()->user->id,
					':type'=>'digg'
				)
			);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}
}